import type { Directive } from "@vue/runtime-core"

const filterLatMess: Directive = {
  beforeMount(el, binding): void {
    const message: string = binding.value;
    if (message) {
      const divEle: HTMLDivElement = document.createElement("div");
      divEle.innerHTML = message;
      // 匹配img标签
      const imgReg: RegExp = /<img.*?(?:>|\/>)/gi;
      if (imgReg.test(message)) {
        const imgs: Array<HTMLImageElement> = Array.from(divEle.querySelectorAll("img"));
        imgs.some(item => item.dataset.key !== '表情') ? el.innerHTML = '[包含图片]' : el.innerHTML = message;
      } else el.innerHTML = message;
    }
  },
};

export default filterLatMess